<template>
  <page-header-wrapper>
    <global-search :list="searchList" @search="handleSearch(false, $event)" @reset="handleSearch(true, $event)" />
    <a-card :bordered="false">
      <s-table
        ref="table"
        :rowKey="(record) => record.ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <div slot="tabAction">
          <a-button v-action:add type="primary" icon="plus" @click="handleForm(true)">新增</a-button>
        </div>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="description" slot-scope="text">
          <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
        </span>
        <div slot="date" slot-scope="text">
          <span v-if="$isEmpty(text)">未发生</span>
          <span v-else>{{ text | moment }}</span>
        </div>
        <a-badge slot="result" slot-scope="text" :status="text === '已发送' ? 'success' : 'default'" :text="text" />
        <span slot="action" slot-scope="text, record">
          <template>
            <!-- <a v-action:info class="m-r-10">查看</a> -->
            <a
              v-action:edit
              v-if="record.DX_FSJG === '未发送'"
              class="m-r-10"
              @click="handleForm(false, record)"
            >修改</a
            >
            <a-popconfirm
              v-if="record.DX_FSJG === '未发送'"
              v-action:delete
              title="确认删除此短信模板?"
              @confirm="handleDel(record.ID)"
            >
              <a class="m-r-10">删除</a>
            </a-popconfirm>
            <a-popconfirm v-if="record.DX_FSJG === '未发送'" v-action:sms title="确认发送短信?" @confirm="handleSend(record.ID)">
              <a class="text-warning">发送</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <sms-form :visible.sync="visibleForm" :propsData="propsData" @success="handleTableRefresh" />
    <detail :visible.sync="visibleDetail" :propsData="propsData" />
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getParameterList, sendSms } from '@/api/sms'
import SmsForm from './components/form.vue'
import Detail from './components/detail.vue'
const columns = [
  {
    title: '序号',
    align: 'center',
    width: 80,
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '短信类别',
    align: 'center',
    dataIndex: 'DX_LB'
  },
  {
    title: '短信内容',
    align: 'center',
    dataIndex: 'DX_NR',
    scopedSlots: { customRender: 'description' }
  },
  {
    title: '发送人',
    align: 'center',
    dataIndex: 'DX_FSR'
  },
  {
    title: '接收人',
    align: 'center',
    dataIndex: 'DX_JSR'
  },
  {
    title: '发送时间',
    align: 'center',
    dataIndex: 'DX_FSSJ',
    scopedSlots: { customRender: 'date' }
  },
  {
    title: '发送结果',
    align: 'center',
    dataIndex: 'DX_FSJG',
    scopedSlots: { customRender: 'result' }
  },
  {
    title: '操作',
    align: 'center',
    width: 260,
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  components: {
    STable,
    Ellipsis,
    SmsForm,
    Detail
  },
  data () {
    this.columns = columns
    return {
      searchList: [
        {
          label: '短信类别',
          fields: 'DX_LB',
          type: 'input',
          props: {
            placeholder: '请输入短信类别'
          }
        },
        {
          label: '短信内容',
          fields: 'DX_NR',
          type: 'input',
          props: {
            placeholder: '请输入短信内容'
          }
        },
        {
          label: '发送人',
          fields: 'DX_FSR',
          type: 'input',
          props: {
            placeholder: '请输入发送人'
          }
        },
        {
          label: '接收人',
          fields: 'DX_JSR',
          type: 'input',
          extra: '多个名称请使用 , 隔开',
          props: {
            placeholder: '请输入接收人'
          }
        },
        {
          label: '接收电话',
          fields: 'DX_JSRDH',
          type: 'input',
          extra: '多个电话请使用 , 隔开',
          props: {
            placeholder: '请输入接收电话'
          }
        },
        {
          label: '发送时间',
          fields: 'rangeTime',
          type: 'range',
          props: {
            valueFormat: 'YYYY-MM-DD'
          }
        }
      ],
      // 查询参数
      queryParam: {
        mode: '',
        userid: this.$store.getters.userid,
        strContent: {}
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getParameterList(requestParameters).then((res) => {
          return res
        })
      },
      visibleForm: false,
      visibleDetail: false,
      propsData: {}
    }
  },
  methods: {
    handleForm (isAdd, row = {}) {
      this.propsData = row
      this.visibleForm = true
    },
    handleInfo (row) {
      this.propsData = row
      this.visibleDetail = true
    },
    handleDel (ID) {
      this.$loading.show({
        tip: '删除中，请稍后...'
      })
      sendSms({
        State: 'delete',
        other_dxfs_Modules: {
          ID
        }
      })
        .then((res) => {
          this.$message.success('短信模板删除成功！')
          this.handleTableRefresh()
        })
        .finally(() => {
          this.$loading.hide()
        })
    },
    handleSend (ID) {
      this.$loading.show({
        tip: '发送中，请稍后...'
      })
      sendSms({
        State: 'send',
        other_dxfs_Modules: {
          ID
        }
      })
        .then((res) => {
          this.$message.success('短信发送成功！')
          this.handleTableRefresh()
        })
        .finally(() => {
          this.$loading.hide()
        })
    },
    handleSearch (refresh, strContent) {
      this.queryParam.strContent = strContent
      this.handleTableRefresh(refresh)
    },
    handleTableRefresh (refresh = false) {
      this.$refs.table.refresh(refresh)
    }
  }
}
</script>
